import Cookies from 'js-cookie'
import { getLoginedExpiresTime } from '@/utils/utils'

const common = {
  state: {
    token: Cookies.get('sqs_token') || '',
    accountId: Cookies.get('sqs_account_id'),
    account: Cookies.get('sqs_account'),
    nickname: Cookies.get('sqs_nickname'),
    isLogin: Cookies.get('sqs_token') ? true : false,

    pageTitle: Cookies.get('sqs_page_title') || '',
    pageKeywords: Cookies.get('sqs_page_keywords') || '',
    pageDescription: Cookies.get('sqs_page_description') || '',

    windowResized: null,

    subTypeName: Cookies.get('sqs_sub_type_name') || ''
  },

  actions: {
    setToken({ commit }, value) {
      commit('setToken', value)
    },

    setIsLogin({ commit }, value) {
      commit('setIsLogin', value)
    },

    setAccountId({ commit }, value) {
      commit('setAccountId', value)
    },

    setAccount({ commit }, value) {
      commit('setAccount', value)
    },

    setNickname({ commit }, value) {
      commit('setNickname', value)
    },

    setPageTitle({ commit }, value) {
      commit('setPageTitle', value)
    },

    setPageKeywords({ commit }, value) {
      commit('setPageKeywords', value)
    },

    setPageDescription({ commit }, value) {
      commit('setPageDescription', value)
    },

    setWindowResized({ commit }, value) {
      commit('setWindowResized', value)
    },

    setSubTypeName({ commit }, value) {
      commit('setSubTypeName', value)
    }
  },
  mutations: {
    setToken(state, value) {
      state.token = value
      Cookies.set('sqs_token', value, { expires: getLoginedExpiresTime() })
    },

    setAccount(state, value) {
      state.account = value
      Cookies.set('sqs_account', value, { expires: getLoginedExpiresTime() })
    },

    setNickname(state, value) {
      state.nickname = value
      Cookies.set('sqs_nickname', value, { expires: getLoginedExpiresTime() })
    },

    setIsLogin(state, value) {
      state.isLogin = value
    },

    setPageTitle(state, value) {
      Cookies.set('sqs_page_title', value)
      state.pageTitle = value
    },

    setPageKeywords(state, value) {
      Cookies.set('sqs_page_keywords', value)
      state.pageKeywords = value
    },

    setPageDescription(state, value) {
      Cookies.set('sqs_page_description', value)
      state.pageDescription = value
    },

    setWindowResized(state, value) {
      state.windowResized = value
    },

    setSubTypeName(state, value) {
      Cookies.set('sqs_sub_type_name', value)
      state.subTypeName = value
    }
  }
}

export default common
